import Vue from 'vue'
import Router from 'vue-router'
//在路由配置文件中引入布局组件
import Main from '@/components/Main'
// 引入类别组件
import Category from '@/components/Category'
// 引入馆藏图书组件
import Book from '@/components/Book'
// 引入借阅图书组件
import Borrow from '@/components/Borrow'
// 引入用户组件
import Users from '@/components/Users'
// 引入用户组件
import Logs from '@/components/Logs'
// 引入登录组件
import Login from '@/components/Login'
// 引入欢迎组件
import Welcome from '@/components/Welcome.vue'
Vue.use(Router)

let router=new Router({
  routes: [{ //定义多个路由
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login,
    },
    {
      path: '/main', //path属性访问组件路径
      name: 'main', // name 属性配置组件名称
      component: Main, // component 属性配置具体组件
      redirect: '/welcome',
      children: [ //children定义子组件
        {
          path: '/welcome',
          component: Welcome
        },

        {
          path: '/category',
          name: 'category',
          component: Category
        },
        {
          path: '/book',
          name: 'book',
          component: Book

        }, {
          path: '/borrow',
          name: 'borrow',
          component: Borrow

        }, {
          path: '/users',
          name: 'users',
          component: Users

        }, {
          path: '/logs',
          name: 'logs',
          component: Logs

        }
      ]
    }
  ]
})

// 定义一个路由器
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){
    next();
  }else{
     if(sessionStorage.getItem("username")){
       next();
     }else{
       alert('你还未登录，请登录')
       next({path:'/login'})
     }
  }
})
export default  router
